package com.ykh.mybbsdemo.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ykh.mybbsdemo.dao.mapper.SysUserMapper;
import com.ykh.mybbsdemo.dao.pojo.SysUser;
import com.ykh.mybbsdemo.service.LoginService;
import com.ykh.mybbsdemo.service.SysUserService;
import com.ykh.mybbsdemo.utils.JWTUtils;
import com.ykh.mybbsdemo.vo.ErrorCode;
import com.ykh.mybbsdemo.vo.LoginUserVo;
import com.ykh.mybbsdemo.vo.Result;
import com.ykh.mybbsdemo.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-08-30
 * Time: 10:18
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    LoginService loginService;

    //根据评论人员的ID查询 作者的信息,并返回
    @Override
    public UserVo findUserVoById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        //防止出现空指针
        if( sysUser ==null) {
            sysUser = new SysUser();
            sysUser.setNickname("");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(sysUser,userVo);
        return userVo;
    }

    @Override
    public SysUser findUserById(long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        //防止出现空指针
        if( sysUser ==null) {
            sysUser = new SysUser();
            sysUser.setNickname("杨克鸿");
        }
        return sysUser;
    }

    //登录时,验证用户名和密码
    @Override
    public SysUser findUser(String account, String password) {
       LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.eq(SysUser::getAccount,account);
       queryWrapper.eq(SysUser::getPassword,password);
       queryWrapper.select(SysUser::getAccount,SysUser::getId,SysUser::getAvatar,SysUser::getNickname);
       //只需要一条
       queryWrapper.last("limit 1");
       return sysUserMapper.selectOne(queryWrapper);
    }

    //根据token获取用户信息
    @Override
    public Result findUserInfoByToken(String token) {
        /**
         * 1. 验证token是否合法
         *    是否为空, 解析是否成功 redis是否存在
         * 2. 如果校验失败 返回错误
         * 3. 如果成功, 返回对应的结果 LoginUserVo
         */
        //1 / 2 . 解析是否成功,成功就返回用户信息
        SysUser sysUser = loginService.checkToken(token);
        if( sysUser==null) {
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }
        //走到这里说明校验成功, 返回对应的需要的 loginUserVo 结果
        //创建loginUserVo 对象
        LoginUserVo loginUserVo = new LoginUserVo();
        //赋值
        loginUserVo.setId(String.valueOf(sysUser.getId()));
        loginUserVo.setNickname(sysUser.getNickname());
        loginUserVo.setAccount(sysUser.getAccount());
        loginUserVo.setAvatar(sysUser.getAvatar());
        return Result.success(loginUserVo);
//        Map<String, Object> map = JWTUtils.checkToken(token);
//        if (map == null){
//            return Result.fail(ErrorCode.NO_LOGIN.getCode(),ErrorCode.NO_LOGIN.getMsg());
//        }
//        String userJson = redisTemplate.opsForValue().get("TOKEN_" + token);
//        if (StringUtils.isBlank(userJson)){
//            return Result.fail(ErrorCode.NO_LOGIN.getCode(),ErrorCode.NO_LOGIN.getMsg());
//        }
//        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
//        LoginUserVo loginUserVo = new LoginUserVo();
//        loginUserVo.setAccount(sysUser.getAccount());
//        loginUserVo.setAvatar(sysUser.getAvatar());
//        loginUserVo.setId(sysUser.getId());
//        loginUserVo.setNickname(sysUser.getNickname());
//        return Result.success(loginUserVo);
    }

    //根据前端传来的账号查找账户
    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.last("limit 1");
        return this.sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(SysUser sysUser) {
        //保存用户 id会自动生成 , 默认生成的id是分布式id
        this.sysUserMapper.insert(sysUser);
    }




}
